package com.amlyc.Lock;
//管成法
public class TestPC {
    public static void main(String[] args) {
        SynContainer synContainer = new SynContainer();
        Producterr producter = new Producterr(synContainer);
        Customer customer = new Customer(synContainer);
        new Thread(producter).start();
        new Thread(customer).start();
    }
}

class Producterr implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {

          container.push(new Chicken(i));

        }
    }
    SynContainer container;

    public Producterr(SynContainer container) {
        this.container = container;
    }

}
class Customer implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {

            container.pop();
        }
    }
    SynContainer container;

    public Customer(SynContainer container) {
        this.container = container;
    }
}
class  Chicken{
    int id;

    public Chicken(int id) {
        this.id = id;
    }
}
class SynContainer{
    Chicken[] chickens = new Chicken[10];
    int count = 0;
    public synchronized void push(Chicken chicken){
        if (count == chickens.length-1) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        chickens[count++] = chicken;
        System.out.println("push"+count);
        this.notifyAll();


    }

    public synchronized void pop(){
        if (count == 0) {
            try {
                this.wait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Chicken chicken = chickens[count];
        System.out.println("pop"+count);
        this.notifyAll();


    }


}

